package com.imd.s9.impl;

import com.imd.base.BusinessException;
import com.imd.s9.entity.TableInfo;
import com.imd.s9.service.IAfterHookService;
import com.imd.s9.util.InitCriteria;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author cuiyu
 * @description
 * @date 2024/7/15 16:51:19
 */
@Service
@Slf4j
public class AfterHookImpl implements IAfterHookService {

    @Resource
    MongoTemplate mongoTemplate;

    @Resource
    InitCriteria initCriteria;

    @Override
    public Object insert(String templateId, Map entity) throws BusinessException {
        switch (templateId) {
            case "683fffdae0d8101bcc0eedd5":
                String projectName = entity.get("项目名称").toString();
                String id = entity.get("id").toString();
                String productNo = entity.get("生产批号").toString();
                Query query = initCriteria.createQuery(Criteria.where("项目名称").is(projectName).and("生产批号").is(productNo));
                List<Document> list = mongoTemplate.find(query, Document.class, TableInfo.checkData);
                List<Document> temp1 = list.stream().filter(t -> t.getString("检验结果").equals("合格")).collect(Collectors.toList());
                List<Document> temp2 = list.stream().filter(t -> t.getString("检验结果").equals("不合格")).collect(Collectors.toList());
                Update up = new Update();
                up.set("检验数量", list.size());
                up.set("合格数", temp1.size());
                up.set("不合格数", temp2.size());
                Query query1 = new Query(Criteria.where("id").is(new ObjectId(id)));
                mongoTemplate.updateFirst(query1, up, TableInfo.checkReport);
                break;
            case "68d650f465fd7f5b6b0c02f7":
                String code = entity.get("柜出厂编号").toString();
                String unit = entity.get("工艺环节").toString();
                up = new Update();
                up.set("当前工序", unit);
                mongoTemplate.updateFirst(new Query(Criteria.where("产品编码").is(code).and("isDelete").ne(1)), up, "entity_68ecd27865fd7f5b6b0c05a2");
                break;
            default:
        }
        return null;
    }

    @Override
    public Object delete(String templateId, String entityId) throws BusinessException {
        return null;
    }

    @Override
    public Object update(String templateId, Map oldV, Map entity) throws BusinessException {
        switch (templateId) {
            case "68410427e0d8101bcc0eee02":
                String res = null;
                if (ObjectUtils.isEmpty(entity.get("测量值"))) {
                    res = "不合格";
                } else {
                    String value = entity.get("测量值").toString();
                    Boolean caculType = (Boolean) entity.get("计算类型");
                    String standard = entity.get("计算标准").toString();
                    if (caculType) {
                        res = standard.equals(value) ? "合格" : "不合格";
                    } else {
                        String[] temp = standard.split(",");
                        String min = temp[0].replace("[", "");
                        String max = temp[1].replace("]", "");
                        res = (new BigDecimal(value).compareTo(new BigDecimal(min)) >= 0 &&
                                new BigDecimal(max).compareTo(new BigDecimal(value)) >= 0) ? "合格" : "不合格";
                    }
                }
                Update up = new Update();
                up.set("检验结果", res);
                up.set("updateTime", new Date());
                up.set("updateTime", entity.get("updateName").toString());
                Query query = initCriteria.createQuery(Criteria.where("_id").is(new ObjectId(entity.get("id").toString())));
                mongoTemplate.updateFirst(query, up, TableInfo.checkData);
                break;
        }
        return null;
    }
}
